package com.goldsign.pay.common.util.aes;


import com.goldsign.pay.common.util.rsa.SigNatrueUtil;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author xiaoxianwen
 *
 */
public class AesUtil {
   public static final String VIPARA = "1269571569321021";
   public static final String KEY = "goldsign20161201";
   public static final String BM = "utf-8";



   /**
    * 字节数组转化为大写16进制字符串
    *
    * @param b
    * @return
    */
   private static String byte2HexStr(byte[] b) {
       StringBuilder sb = new StringBuilder();
       for (int i = 0; i < b.length; i++) {
           String s = Integer.toHexString(b[i] & 0xFF);
           if (s.length() == 1) {
               sb.append("0");
           }

           sb.append(s.toUpperCase());
       }

       return sb.toString();
   }

   /**
    * 16进制字符串转字节数组
    *
    * @param s
    * @return
    */
   private static byte[] str2ByteArray(String s) {
       int byteArrayLength = s.length() / 2;
       byte[] b = new byte[byteArrayLength];
       for (int i = 0; i < byteArrayLength; i++) {
           byte b0 = (byte) Integer.valueOf(s.substring(i * 2, i * 2 + 2), 16)
                   .intValue();
           b[i] = b0;
       }

       return b;
   }


   /**
    * AES 加密
    *
    * @param content
    *            明文
    * @param password
    *            生成秘钥的关键字
    * @return
    */

   public static String aesEncrypt(String content, String password) {
       try {
           IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
           SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");
           Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
           cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
           byte[] encryptedData = cipher.doFinal(content.getBytes(BM));

           return Base64ForA.encode(encryptedData);
//          return byte2HexStr(encryptedData);
       } catch (NoSuchAlgorithmException e) {

       } catch (NoSuchPaddingException e) {

       } catch (UnsupportedEncodingException e) {

       } catch (InvalidKeyException e) {

       } catch (IllegalBlockSizeException e) {

       } catch (BadPaddingException e) {

       } catch (InvalidAlgorithmParameterException e) {
           // TODO Auto-generated catch block

       }

       return null;
   }

   /**
    * AES 解密
    *
    * @param content
    *            密文
    * @param password
    *            生成秘钥的关键字
    * @return
    */

   public static String aesDecrypt(String content, String password) {
       try {
           byte[] byteMi = Base64ForA.decode(content);
//          byte[] byteMi=  str2ByteArray(content);
           IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
           SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");
           Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
           cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
           byte[] decryptedData = cipher.doFinal(byteMi);
           return new String(decryptedData, "utf-8");
       } catch (NoSuchAlgorithmException e) {

       } catch (NoSuchPaddingException e) {

       } catch (InvalidKeyException e) {

       } catch (IllegalBlockSizeException e) {
             e.printStackTrace();
       } catch (BadPaddingException e) {

       } catch (UnsupportedEncodingException e) {

       } catch (InvalidAlgorithmParameterException e) {
           // TODO Auto-generated catch block

       }
       return null;
   }
 public static void main(String[] args) {
     System.out.println("pk原文："+ SigNatrueUtil.PUBLIC_KEY_STR);
    String str = AesUtil.aesEncrypt(SigNatrueUtil.PUBLIC_KEY_STR, AesUtil.KEY);
    System.err.println("原字串："+str);
    String con = "GRyVGbDstWgwLx70XI6xdaJJQpUWLbPyfkQJWNqU4y85PSrldwvj4nkKzt5C yWWvAKKtTudCD0ibrmu3C2Lt/hGN7eJf/W9lho1g6lY/UZIf3z2yeVSPUKMR Bv6k30Vz3SXgWv2ftaeVNsc4bBLmsAEDa5Iv1PHXVnfBT5KNY2VdfMW3izPo a8UFxk7UQHLvSc1DfFTI+lGyRBWxfJEoHHmJcR6J77PjaCwAciE75KppgjhK 3Uecu7fqGyV97d5H+9koziCUd/6m09dngK0dpqN2BUaN06jzd2S0Ly8Jni6f EnHc7dYG7zFoZdCAjXltw43J+3OCnpjusgvhztf2Z5yGkfbYLBgJI//+a6fZ 9+lKJ/Q4xFwjawORZShogsntw3b9dMuo2ztbGQixWcDb+jETrMH1dkg67ihx 5cDv2KOuKxUJQP9UhRei3DTIF3w0V4CU1vR2DdXhgOQVysvvrfyuOZHt16m6 wQCId2q+NS72pIAFtgNOyDzC28FAwzKVZpBerIsoYluF8VRMZJT//g==";
    System.out.println("ios串："+con);
   String pwd = "goldsign20161201";
   System.out.println("解密串："+aesDecrypt(str, pwd));
   System.out.println("ios解密："+aesDecrypt(str, pwd));
   System.out.println("====："+VIPARA.getBytes().toString());
}
}